package com.shenqi.language.service.impl;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.shenqi.language.domain.Uservip;
import com.shenqi.language.mapper.UservipMapper;
import com.shenqi.language.util.TimeParser;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.shenqi.language.mapper.InvitationCodeMapper;
import com.shenqi.language.domain.InvitationCode;
import com.shenqi.language.service.IInvitationCodeService;

/**
 * 激活码Service业务层处理
 *
 * @author shenqi
 * @date 2025-08-08
 */
@Service
public class InvitationCodeServiceImpl implements IInvitationCodeService {
    @Autowired
    private InvitationCodeMapper invitationCodeMapper;

    @Autowired
    private UservipMapper uservipMapper;

    /**
     * 查询激活码
     *
     * @param userId 激活码主键
     * @return 激活码
     */
    @Override
    public InvitationCode selectInvitationCodeByUserId(Long userId) {
        return invitationCodeMapper.selectInvitationCodeByUserId(userId);
    }

    /**
     * 查询激活码列表
     *
     * @param invitationCode 激活码
     * @return 激活码
     */
    @Override
    public List<InvitationCode> selectInvitationCodeList(InvitationCode invitationCode) {
        return invitationCodeMapper.selectInvitationCodeList(invitationCode);
    }

    /**
     * 新增激活码
     *
     * @param invitationCode 激活码
     * @return 结果
     */
    @Override
    public int insertInvitationCode(InvitationCode invitationCode) {
        String code = null;
        while (true) {
            code = RandomStringUtils.randomAlphabetic(8).toUpperCase();
            InvitationCode found = invitationCodeMapper.selectInvitationCodeByCode(code);
            if (found == null) {
                break;
            }
        }
        invitationCode.setCode(code);
        invitationCode.setCreateTime(DateUtils.getNowDate());
        return invitationCodeMapper.insertInvitationCode(invitationCode);
    }

    /**
     * 修改激活码
     *
     * @param invitationCode 激活码
     * @return 结果
     */
    @Override
    public int updateInvitationCode(InvitationCode invitationCode) {
        invitationCode.setUpdateTime(DateUtils.getNowDate());
        return invitationCodeMapper.updateInvitationCode(invitationCode);
    }

    /**
     * 批量删除激活码
     *
     * @param userIds 需要删除的激活码主键
     * @return 结果
     */
    @Override
    public int deleteInvitationCodeByUserIds(Long[] userIds) {
        return invitationCodeMapper.deleteInvitationCodeByUserIds(userIds);
    }

    /**
     * 删除激活码信息
     *
     * @param userId 激活码主键
     * @return 结果
     */
    @Override
    public int deleteInvitationCodeByUserId(Long userId) {
        return invitationCodeMapper.deleteInvitationCodeByUserId(userId);
    }

    @Override
    public boolean checkCodeValid(String invitationCode) {
        boolean valid = invitationCodeMapper.checkCodeValid(invitationCode);
        return valid;
    }

    @Override
    public void useByUserId(String invitationCode, Long userId) {
        // 设置这个码被用户使用了。
        InvitationCode code = invitationCodeMapper.selectInvitationCodeByCode(invitationCode);
        code.setUserId(userId);
        invitationCodeMapper.updateInvitationCode(code);

    }

    @Override
    public InvitationCode selectInvitationCodeByCode(String code) {
        return invitationCodeMapper.selectInvitationCodeByCode(code);
    }
}
